package tt.dz.service.imp.base;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tt.dz.dao.base.OmsCommonDao;
import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.base.OmsCommonParam;
import tt.dz.entity.base.OmsPartnerType;
import tt.dz.entity.exception.OmsDaoException;
import tt.dz.service.base.PartnerTypeService;
import tt.dz.util.OmsConstant.MsgCodeEnum;
@Service @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class PartnerTypeServiceImpl extends BaseService implements PartnerTypeService{
	@Autowired
	@Qualifier("base")
	private OmsCommonDao<OmsPartnerType> omsCommonDaoImpl;
	
	@Override
	public   JsonCommon<?> add(OmsPartnerType partnerType) 
	{
		try 
		{
			omsCommonDaoImpl.add(partnerType);
			return this.buildSuccessJsonCommon(partnerType);
		} catch (OmsDaoException e) 
		{
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

	@Override
	public JsonCommon<?> update(OmsPartnerType partnerType) 
	{
		if (partnerType.getType_id() == null || partnerType.getType_id() <= 0)
		{
			return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:the request id is empty");
		}
		try {
			omsCommonDaoImpl.update(partnerType);
			return this.buildSuccessJsonCommon(partnerType);
		} catch (OmsDaoException e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

	@Override
	public JsonCommon<?> delete(String id) {
		if (StringUtils.isEmpty(id))
		{
			return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:the request id is empty");
		}
		try {
			omsCommonDaoImpl.deleteById(id, OmsPartnerType.class);
			return this.buildSuccessJsonCommon("success");
		} catch (OmsDaoException e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

	@Override
	public JsonCommon<?> findById(String id) {
		if (StringUtils.isNumeric(id))
		{
			return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:the request id is empty");
		}
		try 
		{
			OmsPartnerType partnerType = new OmsPartnerType();
			partnerType.setType_id(Integer.parseInt(id));
			OmsPartnerType op = omsCommonDaoImpl.findRecordByPK(partnerType);
			return this.buildSuccessJsonCommon(op);
		} 
		catch (Exception e) 
		{
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

	@Override
	public JsonForList<?> findPageObjs(OmsCommonParam param, OmsPartnerType partnerType) 
	{
		try 
		{
			List<OmsPartnerType> lst = omsCommonDaoImpl.pagingFind(param, partnerType);
			int total = 0;
			if (param.isQueryTotal())
			{
				total = omsCommonDaoImpl.queryTotalByObject(partnerType);
			}
			return this.buildSuccessJsonForList(param, lst, total);
		} catch (Exception e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonForList();
		}
	}

	@Override
	public List<OmsPartnerType> findAll() {
		
		return this.omsCommonDaoImpl.findAll(new OmsPartnerType());
	}

}
